home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
labyte.exe
/
LABYTE.DOC
< prev
next >
Wrap
Text File
|
1992-05-08
|
20KB
|
482 lines
Memory Manager
Version S1.1
One of the most difficult parts of programming in C is handling
memory and its pointers. At least, that is what I found. To help me
reduce these problems, I wrote LAByte.
The functions provided are:
1. Handling the allocation of memory blocks, freeing the blocks,
checking for memory corruption.
2. Provide a method of tracing the entrance into each function.
NOTE: There are two separate traces maintained by the module. The
first is for the memory functions and the second is for
function tracing.
Memory Processor Functions
All calls are "traced". If an error occurs the trace table is written
to a file named LATrace.LAC. Each of the function calls use the file
name and line number. You may supply your own file name as a
character pointer or use the __FILE__ symbol, likewise, you may use
__LINE__ symbol for the line number. More on the trace later.
Allocation of Memory
Memory is allocated from the far heap. A far character pointer is
returned pointing to the first usable byte of the block. The block
is initialized to NULLs.
prototype:
char far *mem_getmain(char *File, int Line, char Fun1, char Fun2, int Size);
char *File = A name that will point you to the module doing the
getmain. You may use the __FILE__ symbol.
int Line = A number representing the place in the module where the
getmain was called. You may use the __LINE__ symbol.
char Fun1 = These characters can be set to anything that will help
char Fun2 point you in the right direction. I use the selection
characters from the main menu and its pull down menu.
int Size = The size of the memory block to be allocated.
example:
int Samp_Fun()
{
char far *Cnfg;
Cnfg = mem_getmain(__FILE__, __LINE__, 'M', 'C', sizeof(CONFIG_DATA));
}
Freeing A Memory Block
When called, freemain will verify the integrity of the block
passed, abending if it is not valid or freeing the block and
returning to the calling return. This function can only be
used to free memory blocks allocated by the getmain function
because of the trace chain and block integrity header and
trailer.
prototype:
int mem_freemain(char *File, int Line, char far *Pointer);
char *File = A name that will point you to the module doing the
getmain. You may use the __FILE__ symbol.
int Line = A number representing the place in the module where the
getmain was called. You may use the __LINE__ symbol.
char far *Pointer = The pointer returned from the call to getmain.
example:
int Samp_Fun()
{
char far *Cnfg;
mem_freemain(__FILE__, __LINE__, Cnfg);
}
Freeing Memory Blocks By A Function
When called, cleanup will verify the integrity of all blocks
on the chain where the passed function matches the char passed in
Fun1 of the getmain, abending if an invalid is encountered or
freeing the block and returning to the calling return. The reason
for using cleanup is to eliminate the build up of unreleased memory
as a function ends or as a quick and easy way to release all memory
that was allocated to accomplish the function.
prototype:
int mem_cleanup(char *File, int Line, char Fun);
char *File = A name that will point you to the module doing the
getmain. You may use the __FILE__ symbol.
int Line = A number representing the place in the module where the
getmain was called. You may use the __LINE__ symbol.
char Fun = The character passed in Fun1 of the getmain for the
blocks to be released.
example:
int Samp_Fun()
{
mem_cleanup(__FILE__, __LINE__, 'M');
}
Freeing All Memory Blocks
When called, freeall will verify the integrity of all blocks
on the chain, abending if invalid or freeing the blocks and
returning to the calling return.
prototype:
int mem_freeall(char *File, int Line);
char *File = A name that will point you to the module doing the
getmain. You may use the __FILE__ symbol.
int Line = A number representing the place in the module where the
getmain was called. You may use the __LINE__ symbol.
example:
int Samp_Fun()
{
mem_freeall(__FILE__, __LINE__);
}
Memory Trace Entry
Internal table
Memory trace entries are allocated from the near heap as
memory is allocated and added to the chain. The first trace
entry address is saved in the anchor in LAByte.C named
MemAnchor.
typedef struct {
int MemOK; /* Normally NULL but set to a value
if an error is detected. see
Error Codes below */
char MemMod[15]; /* File Name from getmain */
int MemLine; /* Line Number from getmain */
char MemFun; /* Fun1 from getmain */
char MemUse; /* Fun2 from getmain */
int MemLen; /* Length from getmain */
void far *MemAddr; /* Address of the memory block */
void *MemPrev; /* Address of the previous memory trace
entry or the memory anchor in the
header */
void *MemNext; /* Address of the next memory trace
entry or null if this is the last
entry on the chain */
} MEMTBL;
LATrace.Lac File Entry (see Cancelling The Program below)
M---5D8D:0004---0---main.c---25---M---C---365---5D75:0008---6C60:0004---0000:0000
^-A ^-B ^-C ^-D ^-E ^-F ^-G ^-H ^-I ^-J ^-K
A) Indicator showing this as a memory trace entry
B) Address of this trace entry
C) Error Code, see Error Codes below
D) File Name
E) Line Number
F) Fun1
G) Fun2
H) Length of the memory block
I) Address of the memory block
J) Address of previous memory trace entry
K) Address of next memory trace entry
Error codes
mem_freemain function
1 - There is no memory on the trace chain
2 - Can't find find the memory block on the chain
3 - The backward chain has been corrupted
4 - The forward chain has been corrupted
5 - The header of the memory block has been corrupted
6 - The trailer of the memory block has been corrupted
mem_cleanup function
11 - The backward chain has been corrupted
12 - The header of a memory block has been corrupted
13 - The trailer of a memory block has been corrupted
mem_freeall function
21 - The backward chain has been corrupted
22 - The header of a memory block has been corrupted
23 - The trailer of a memory block has been corrupted
If the trailer of a memory block has been corrupted,
error codes 6, 13 and 23, probably the definition of the area
is larger than the memory block allocated.
If the header is corrupted, error codes 5, 12 and 22,
either the pointer has been changed, an array was
improperly indexed or the previous area was overrun.
For all other error codes, the memory trace chain has
been corrupted